<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 10/24/12
 * Time: 8:37 AM
 * To change this template use File | Settings | File Templates.
 */
class Controllers_News extends Libs_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->model= new Models_News();
    }
    public function index($param=null)
    {
        $this->view->listnews=$this->model->getnews($param[0]);
        $this->view->render('news/index');
        if($_POST['ItemDel']) {
            //$action='';
            if($_POST['element']=='') {

                echo '<script>alert(\'Mời bạn chọn ít nhất 1 bản tin\')</script>';
                return;
            }
            else{

                $id=implode(",",$_POST['element']);

                $this->model->delnews($id);


                echo '<script>location.reload();</script>';
            }
        }

    }
    public function detail($param=null)
    {
        $this->view->news=$this->model->getnewdetail($param[0]);
        $this->view->render("news/detail");
        if($_POST['sua'])
        {
            $tieude=$_POST['tieude'];
            $tomtat=$_POST['tomtat'];
            $anh=$_POST['anh'];
            $trangthaitin=$_POST['trangthaitin'];
            $noidungtin=$_POST['noidungtin'];
            $this->model->updatenews($param[0],$tieude,$tomtat,$anh,$trangthaitin,$noidungtin);
            echo '<script type="text/javascript">alert("Cập nhật thành công" )</script>';
            echo '<script type="text/javascript">location.reload();</script>';


        }
    }
    public function add()
    {
        $this->view->render('news/add');
        if($_POST['dangtin'])
        {
            $tieude=$_POST['tieude'];
            $tomtat=$_POST['tomtat'];
            $anh=$_POST['anh'];
            $trangthaitin=$_POST['trangthaitin'];
            $noidungtin=$_POST['noidungtin'];
            $this->model-> insertnews($tieude,$tomtat,$anh,$trangthaitin,$noidungtin);
            echo '<script type="text/javascript">alert("Đăng tin thành công" )</script>';
        }
    }
}
